/*********************************************************
 *  Copyright (c) 2011 by Web Information Systems (WIS) Group, 
 *  Delft University of Technology.
 *  Qi Gao, http://wis.ewi.tudelft.nl/index.php/home-qi-gao
 *  
 *  Some rights reserved.
 *
 *  Contact: q.gao@tudelft.nl
 *
 **********************************************************/
package org.persweb.genius.weight;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.persweb.genius.model.Concept;
import org.persweb.genius.model.Item;

/**
 * @author Qi Gao <a href="mailto:q.gao@tudelft.nl">q.gao@tudelft.nl</a>
 * @version created on Jul 4, 2011 3:55:43 PM
 */
public class TimeSensitiveTF implements WeightFunction {
	
	private Map<Concept, Timestamp> conceptsMap = null;

	/**
	 * 
	 */
	public TimeSensitiveTF() {
		super();
	}

	/**
	 * @param conceptsMap
	 */
	public TimeSensitiveTF(Map<Concept, Timestamp> conceptsMap) {
		super();
		this.conceptsMap = conceptsMap;
	}

	public Map<Concept, Timestamp> getConceptsMap() {
		return conceptsMap;
	}

	public void setConceptsMap(Map<Concept, Timestamp> conceptsMap) {
		this.conceptsMap = conceptsMap;
	}

	/* (non-Javadoc)
	 * @see org.persweb.genius.weight.WeightFunction#calculateWeights(java.util.Collection)
	 */
	@Override
	public Map<Concept, Double> calculateWeights(
			Collection<Item> items) {
		HashMap<Concept, Double> weightedConcepts = new HashMap<Concept, Double>();
		System.out.println("calculating weights...");
		List<Timestamp> timestamps = new ArrayList<Timestamp>();
		
		//get the time frame
		for(Item item : items) {
			timestamps.add(item.getCreationTime());
		}
		Timestamp to = Collections.max(timestamps);
		Timestamp from = Collections.min(timestamps);
		
		for(Item item : items){
			Timestamp creationTime = item.getCreationTime();
			double weight =  1.0 - (double) (to.getTime() - creationTime.getTime())/(to.getTime() - from.getTime());
			List<Concept> concepts = item.getConcepts();
			for(Concept concept : concepts){
				if(weightedConcepts.containsKey(concept)){
					System.out.println("repeated concept: " + concept.getUri().toString());
					weightedConcepts.put(concept, weightedConcepts.get(concept) + weight);
				} else{
					weightedConcepts.put(concept, weight);
				}
			}
		}
		return weightedConcepts;
	}
}
